import {RECEIVE_USERID, RESET_USERID, RECEIVE_TOKEN, RESET_TOKEN} from '../mutations-types'

const state = {
  id: '',
  token: ''
}
const mutations ={
  [RECEIVE_TOKEN](state, token){
    state.token = token
  },

  [RECEIVE_USERID](state, id){
    state.token = id
  },

  [RESET_USERID](state){
    state.id = ''
    localStorage.removeItem("id")
  },

  [RESET_TOKEN](state){
    state.token = ''
    localStorage.removeItem("token")
  },
}
const actions ={
  save_user({commit}, registerInfo){
    localStorage.setItem('token', JSON.stringify('Bearer ' + registerInfo.objtoken))
    localStorage.setItem('id', JSON.stringify(registerInfo.id))
    commit(RECEIVE_USERID, registerInfo.id)
    commit(RECEIVE_TOKEN, 'Bearer ' + registerInfo.objtoken)
  },
  save_loginUser({commit}, loginInfo){
    localStorage.setItem('token', JSON.stringify('Bearer ' + loginInfo.token))
    localStorage.setItem('id', JSON.stringify(loginInfo.id))
    commit(RECEIVE_USERID, loginInfo.id)
    commit(RECEIVE_TOKEN, 'Bearer ' + loginInfo.token)
  },

  resetUser({commit}){
    commit(RESET_USERID)
    commit(RESET_TOKEN)
  }
}
const getters ={}

export default{
  state,
  mutations,
  actions,
  getters
}